package com.code2roc.jarmanage.servlet.redis;

import com.code2roc.jarmanage.model.RedisDataBaseInfo;
import com.code2roc.jarmanage.model.Result;
import com.code2roc.jarmanage.util.CommonUtil;
import com.code2roc.jarmanage.util.JedisUtil;
import com.code2roc.jarmanage.util.LogUtil;
import redis.clients.jedis.Jedis;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class GetDataBaseInfoServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Result result = Result.okResult();
        List<RedisDataBaseInfo> redisDataBaseInfoList = new ArrayList<>();
        Jedis client = JedisUtil.getClient();
        try{
            for (int i = 0; i <= 15; i++) {
                int keyCount = JedisUtil.getKeys("*",client, i,false).size();
                redisDataBaseInfoList.add(new RedisDataBaseInfo(i, "db" + i, keyCount));
            }
        }catch (Exception e){
            LogUtil.error(e);
            throw e;
        }finally {
            client.close();
        }
        result.add("obj", redisDataBaseInfoList);
        resp.getWriter().println(result.toString());
    }
}
